package github.flandre.examplemod.client.gui;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import github.flandre.examplemod.examplemod;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.gui.widget.Widget;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.client.settings.SliderPercentageOption;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;

public class ExampleSimpleGui extends Screen {
    //文本框
    TextFieldWidget textFieldWidget;
    //按钮
    Button button;
    //gui的图片
    ResourceLocation SIMPLE_TEXTURES =
            new ResourceLocation(examplemod.MODID,"textures/gui/xxx.png");
    //贴图尺寸
    int textureWidth = 229;
    int textureHeight = 162;
    //汉化类 可以在汉化中 example.simple.gui 进行汉化
    TranslationTextComponent content = new TranslationTextComponent(
            "example.simple.gui"
    );
    //滑动的栏
    SliderPercentageOption option;
    //滑动块
    Widget SliderBar;


    protected ExampleSimpleGui(ITextComponent titleIn)
    {
        super(titleIn);
    }

    @Override
    public void init(Minecraft minecraft, int width, int height) {
        super.init(minecraft, width, height);
        //监听键盘输入事件
        this.minecraft.keyboardListener.enableRepeatEvents(true);
        //创建文本框
        this.textFieldWidget = new TextFieldWidget(this.font,
                this.width/2-100,
                66,200,20,new TranslationTextComponent("gui"+examplemod.MODID+"first_context"));
        this.children.add(this.textFieldWidget);

        this.button = new Button(this.width / 2 - 40, 96, 80, 20, new TranslationTextComponent("gui." + examplemod.MODID + ".first.save"), (button) -> {
        });
        this.addButton(button);

        this.option = new SliderPercentageOption(examplemod.MODID + ".sliderbar", 5, 100, 5,
                (setting) -> Double.valueOf(0), (setting, value) -> {},
                (gameSettings, sliderPercentageOption1) -> new TranslationTextComponent("gui." + examplemod.MODID + ".first.test"));
        this.SliderBar = this.option.createWidget(Minecraft.getInstance().gameSettings, this.width / 2 - 100, 120, 200);
        this.children.add(this.SliderBar);

    }

    //渲染东西的写到这里
    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {

        super.render(matrixStack, mouseX, mouseY, partialTicks);
        this.renderBackground(matrixStack);
        //初始化画笔
        RenderSystem.color4f(1.0f,1.0f,1.0f,1.0f);
        //绑定渲染图片
        this.minecraft.getTextureManager().bindTexture(SIMPLE_TEXTURES);
        int posX = (this.width/2)-(textureWidth/2);
        int posY = (this.height/2)-(textureHeight/2);
        this.blit(matrixStack,
                posX,posY, //渲染的位置
                0,0, //贴图坐标
                textureWidth, textureHeight, //框的大小
                textureWidth, textureHeight  //图片的大小
                );
        drawCenteredString(matrixStack,this.font,content,this.width/2-10,30,0xeb0505);

        this.textFieldWidget.render(matrixStack,mouseX,mouseY,partialTicks);
        this.button.render(matrixStack,mouseX,mouseY,partialTicks);
        this.SliderBar.render(matrixStack,mouseX,mouseY,partialTicks);
        super.render(matrixStack,mouseX,mouseY,partialTicks);
    }
}
